package sd.socket.tcp.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import sd.socket.interfaces.IService;

public class ServiceServerProxy {
	private IService service;
	private int port;

	public ServiceServerProxy(IService service, int port) {
		this.setService(service);
		this.setPort(port);
	}


	public void run() {
		String s = null;
		ProcessBuilder pb = new ProcessBuilder();
		pb.directory(new File("/home/v/sd/socket/socket_2/"));
		pb.command(new String[] {"./server.o"});
		pb.redirectErrorStream(true);
		
		Process p;
		
		try {
			p = pb.start();
			
			
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

			BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

        	// read the output from the command
       	    while ((s = stdInput.readLine()) != null) {
         		System.out.println(s);
            }
            
        	// read any errors from the attempted command
    		while ((s = stdError.readLine()) != null) {
        		System.out.println(s);
    		}

    		stdInput.close();
    		stdError.close();
    		
    		p.destroy();
    		
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public void setService(IService service) {
		this.service = service;
	}


	public IService getService() {
		return service;
	}


	public void setPort(int port) {
		this.port = port;
	}


	public int getPort() {
		return port;
	}

}
